//
//  PPCreateGroupViewController.m
//  PetParadise
//
//  Created by 陈无忌 on 9/5/14.
//  Copyright (c) 2014 whalefin. All rights reserved.
//

#import "PPCreateGroupViewController.h"
#import "PPInviteFriendTableViewController.h"
#import "supportingHeadFiles.h"
#import "PPSystemConfiguraton.h"

@interface PPCreateGroupViewController ()<UITextFieldDelegate,UITextViewDelegate>
@property (weak, nonatomic) IBOutlet UITextField *groupNameTF;
@property (weak, nonatomic) IBOutlet UITextView *IntroductionTV;

@property (strong, nonatomic) EMGroupStyleSetting *setting;
@property (weak, nonatomic) PPSystemConfiguraton *config;
@property (weak, nonatomic) IBOutlet UILabel *promptLabel;

@end

@implementation PPCreateGroupViewController
-(EMGroupStyleSetting *)setting
{
    if (!_setting) {
        _setting = [[EMGroupStyleSetting alloc] init];
        _setting.groupStyle = eGroupStyle_PublicOpenJoin;
    }
    return _setting;
}
-(PPSystemConfiguraton *)config
{
    if (!_config) {
        _config = [PPSystemConfiguraton sharedSystemConfiguration];
    }
    return _config;
}


- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.groupNameTF.delegate = self;
    self.IntroductionTV.delegate = self;
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(createGroup:) name:@"getInviteMember" object:nil];
    // Do any additional setup after loading the view.
}

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}



- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)createGroup:(NSNotification *)notification
{
    [HUD showUIBlockingIndicatorWithText:@"正在创建"];
    NSDictionary *dic = [notification userInfo];
    NSString *inviteString = dic[@"memberIdList"]; //自己的服务器使用
    NSString *afterString = [NSString stringWithFormat:@",%d",self.config.userInfo.id];
    inviteString = [inviteString stringByAppendingString:afterString];

    NSMutableArray *inviteArray = dic[@"memberIdArray"];//环信创建群组使用
    [[EaseMob sharedInstance].chatManager asyncCreateGroupWithSubject:self.groupNameTF.text description:self.IntroductionTV.text invitees:inviteArray initialWelcomeMessage:@"欢迎大家" styleSetting:self.setting completion:^(EMGroup *group, EMError *error) {
        //code
        if (group && !error) {
            NSString *url = [PPAPIList getAPIList].PPCreateGroup;
            NSDictionary *params = @{@"easeMobGroupId":group.groupId,@"groupName":group.groupSubject,@"userIdString":inviteString};
            [httpRequst POST:url parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
                NSNumber *processResult = responseObject[@"processResult"];
                [HUD hideUIBlockingIndicator];
                if (processResult.boolValue) {
                    [PPHUD hudShowWithString:@"创建群组成功" andView:self.view];
                    [self.navigationController popViewControllerAnimated:YES];
                }else{
                    [PPHUD hudShowWithString:@"创建群组失败，请重新操作" andView:self.view];
                }
            } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
                [PPHUD hudShowWithString:@"网络错误，请重新操作" andView:self.view];
            }];
        }
        else{
            [HUD hideUIBlockingIndicator];
            [PPHUD hudShowWithString:@"创建群组失败，请重新操作" andView:self.view];
        }
    } onQueue:nil];

    
}


-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
    if ([textField isEqual:self.groupNameTF]) {
        [self.groupNameTF resignFirstResponder];
    }
    return YES;
}

-(BOOL)textViewShouldBeginEditing:(UITextView *)textView{
    self.promptLabel.hidden = YES;
    return YES;
}

-(BOOL)textViewShouldEndEditing:(UITextView *)textView{
    if (!textView.text.length > 0) {
        self.promptLabel.hidden = NO;
    }
    return YES;
}



-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text

{
    if ([text isEqualToString:@"\n"]) {
        
        [textView resignFirstResponder];
        return NO;
    }
    return YES;
    

}


- (IBAction)inviteMember:(UIBarButtonItem *)sender {
    if (!self.groupNameTF.text.length > 0) {
        [PPHUD hudShowWithString:@"请输入群聊名!" andView:self.view];
        return;
    }
    if (!self.IntroductionTV.text.length > 0) {
        [PPHUD hudShowWithString:@"请输入群聊简介!" andView:self.view];
        return;
    }
    PPInviteFriendTableViewController *targetVC = [self.storyboard instantiateViewControllerWithIdentifier:@"PPInviteFriendTableViewController"];
    [self.navigationController pushViewController:targetVC animated:YES];
    
    
}

-(void)alertShow :(NSString *)string
{
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:string delegate:nil cancelButtonTitle:@"确定" otherButtonTitles: nil];
    [alert show];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
